"""
@author: peng
@email: sb.peng@hzgosun.com
@file: electricFenceTest.py
@time: 2019/5/23 11:42

后台电子围栏管理的增删改查自动化测试用例编写
"""
import unittest
from time import sleep

from selenium.webdriver.common.keys import Keys

from common.functions import save_screenshot, get_data_file_abs_path
from common.decorators import mkdir_classname, log4gs
from common.settings import TAG, DATA_DIR
from common.vue.select import DropDownSelect,OperateSelect
from pages.electricFencePage import ElectricFencePage


@log4gs
@mkdir_classname(TAG)
class ElectricFenceTest(unittest.TestCase):
    """电子围栏管理功能测试"""
    @classmethod
    def setUpClass(cls):
        cls.log.info('=' * 30 + "开始电子围栏管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page = ElectricFencePage('/DevManage/EleFenceManage')

    @classmethod
    def tearDownClass(cls):
        cls.log.info('=' * 30 + "结束电子围栏管理功能测试({})".format(__class__.__name__) + '=' * 30)
        cls.page.driver.quit()

    def setUp(self):
        self.log.info('>>>>>开始执行用例：{} {}'.format(self.id(), self.shortDescription()))

    def tearDown(self):
        self.log.info('<<<<<结束执行用例：{} {}'.format(self.id(), self.shortDescription()))

    @unittest.skip
    def test_1_turn_page(self):
        """翻页功能
        http://181.181.0.216:8786/tc/detail/74-4509/
        """
        self.log.debug("当前共有：" + self.page.turnPage.total_page +"页数据" )
        self.log.debug("当前处于第：" + self.page.turnPage.current_page + "页数据")
        self.assertEqual(self.page.turnPage.current_page, '1')
        sleep(1)
        try:
            self.assertNotEqual(self.page.turnPage.total_page, 1, msg="页数过少，无法进行翻页测试")
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')

            sleep(1)
            self.page.turnPage.next_page.click()
            self.assertEqual(self.page.turnPage.current_page, '3')

            sleep(1)
            self.page.turnPage.prev_page.click()
            self.assertEqual(self.page.turnPage.current_page, '2')

            sleep(1)
            self.page.turnPage.input.clear()
            self.page.turnPage.input.send_keys(6)
            self.page.turnPage.input.send_keys(Keys.ENTER)
            self.assertEqual(self.page.turnPage.current_page, '6')
        except Exception:
            self.log.error("当前模块最大为1页，无法进行翻页测试", exc_info=True)
            save_screenshot('翻页功能测试失败')

    @unittest.skip
    def test_search_1(self):
        """序列号查询"""
        self.page.searchTools.NumberWord_input.send_keys('CTSC1001')
        self.page.searchTools.search_btn.click()  # 点击检索按钮
        sleep(1)
        result = self.page.table.get_row("CTSC1001")
        self.assertTrue(result, "通过序列号:查到了CTSC1001")
        sleep(2)
        try:
            self.assertTrue(result)
            self.log.debug(result)
        except:
            save_screenshot(self, "截图示例")
            sleep(2)

    @unittest.skip
    def test_search_2(self):
        """组合查询"""
        self.page.searchTools.NumberWord_input.send_keys('JQDW001')
        sleep(3)
        # DropDownSelect(self.page.searchTools.region_dropdown_list, "区域")
        self.page.searchTools.region_region_input.click()

        self.page.searchTools.region_region_selector.select("山西省-太原市-小店区")

        DropDownSelect(self.page.searchTools.community_dropdown_list, "太原晋泉小区")
        self.page.searchTools.search_btn.click()
        sleep(1)
        result = self.page.table.get_row("晋泉小区1")
        self.assertTrue(result, "通过查询:晋泉小区1")
        sleep(1)
        save_screenshot(self, "截图示例")
        sleep(1)

    #@unittest.skip
    def test_add_3(self):
        """添加电围"""
        self.page.table.add_btn.click()
        sleep(2)
        self.page.addFence.add_fenceName.send_keys('自动化测试01')
        self.page.addFence.add_fenceNumber.send_keys('ADSFS324225')
        sleep(1)
        DropDownSelect(self.page.addFence.add_fenceStyle,"坤杨")
        sleep(1)
        DropDownSelect(self.page.addFence.add_equipFence,"固定点位")
        sleep(1)
        self.page.addFence.add_FenceRegion.click()
        self.page.addFence.add_FenceRegion_selector.select("山西省-太原市-小店区")

        sleep(1)

        DropDownSelect(self.page.addFence.add_FenceCommunity, "太原兴达小区")
        sleep(2)
        self.page.addFence.add_FenceIp.send_keys('1.3.56.78')
        sleep(1)  # 等待网格选择器收起完毕
        self.page.addFence.confirm.click()
        sleep(1)
        self.page.searchTools.NumberWord_input.send_keys('ADSFS324225')
        self.page.searchTools.search_btn.click()  # 点击检索按钮
        sleep(1)
        result = self.page.table.get_row("ADSFS324225")
        try:
            self.assertTrue(result, msg="通过查询:ADSFS324225,查询到ADSFS324225")
            self.log.info("添加电围ADSFS324225用例执行成功")
        except Exception:
            self.log.error("添加电围ADSFS324225用例执行失败", exc_info=True)
            save_screenshot('添加电围ADSFS324225用例执行失败')
        sleep(1)

    #@unittest.skip
    def test_change_add_4(self):
        """修改电围"""
        self.page.searchTools.NumberWord_input.clear()
        self.page.searchTools.NumberWord_input.send_keys('ADSFS324225')  # 输入电围，和page页中对应是camera_input
        self.page.searchTools.search_btn.click()  # 点击检索按钮

        sleep(1)
        search_result = self.page.table.get_row("ADSFS324225")
        sleep(1)
        OperateSelect(search_result, '查看')
        # self.log.debug(search_result)
        self.page.modifyFence.change_btn.click()
        sleep(2)
        self.page.modifyFence.add_fenceName.clear()
        sleep(1)
        self.page.modifyFence.add_fenceName.send_keys("自动化测试修改")

        self.page.modifyFence.add_fenceNumber.clear()
        sleep(1)
        self.page.modifyFence.add_fenceNumber.send_keys("lkjhgfds")
        sleep(2)
        DropDownSelect(self.page.modifyFence.add_fenceStyle, "码牛")
        sleep(1)
        DropDownSelect(self.page.modifyFence.add_equipFence, "移动采集")
        self.page.modifyFence.confirm.click()

        # 检查是否修改成功
        sleep(1)  # 等待弹窗消失完毕
        self.page.searchTools.NumberWord_input.clear()
        sleep(1)
        self.page.searchTools.NumberWord_input.send_keys("lkjhgfds")
        self.page.searchTools.search_btn.click()  # 点击检索
        sleep(2)

        search_result = self.page.table.get_row("lkjhgfds")
        try:
            self.assertTrue(search_result, msg="找到摄像机型号，查询成功")
            self.log.info("修改电围用例执行成功")
        except Exception:
            self.log.error("修改电围用例执行失败", exc_info=True)
            save_screenshot('修改电围用例执行失败')
        # self.page.searchTools.reset_btn.clear()
        sleep(1)

    #@unittest.skip
    def test_delect_5(self):
        """删除电围"""
        self.page.searchTools.NumberWord_input.clear()
        self.page.searchTools.NumberWord_input.send_keys("lkjhgfds")  # 输入摄像机型号，和page页中对应是camera_input
        self.page.searchTools.search_btn.click()  # 点击检索按钮
        sleep(2)
        result = self.page.table.get_row("lkjhgfds")
        OperateSelect(result, "删除")
        sleep(1)
        self.page.makeSure.confirm.click()

        # delete_result = self.page.table.get_row("lkjhgfds")
        # try:
        #     self.assertFalse(delete_result, msg="删除电围用例执行失败")
        #     self.log.info("删除电围用例执行失败")
        # except Exception:
        #     self.log.error("删除摄像机用例执行成功", exc_info=True)
        #     save_screenshot('删除摄像机用例执行成功')
        # self.assertFalse(delete_result, "查询bu到lkjhgfds")
        # # self.page.searchTools.reset_btn.click()  # 点击重置按钮
        # sleep(1)
        # save_screenshot(self, "截图示例")
        # sleep(1)

    @unittest.skip
    def test_import_6(self):
        """导入电围"""
        self.page.table.import_btn.click()
        sleep(1)
        self.page.importFence.option_xlsx.send_keys(get_data_file_abs_path("detector_template.xlsx"))
        sleep(6)
        self.page.importFence.upload.click()
        sleep(5)
        self.page.importFence.shut_down.click()
        # 检查导入是否成功
        self.page.searchTools.NumberWord_input.send_keys('JQDW0010')
        self.page.searchTools.search_btn.click()  # 点击检索按钮
        sleep(1)
        result = self.page.table.get_row("JQDW0010")
        try:
            self.assertTrue(result, msg="通过查询:JQDW0010,查询到JQDW0010")
            self.log.info("导入电围JQDW0010用例执行成功")
        except Exception:
            self.log.error("导入电围JQDW0010用例执行失败", exc_info=True)
            save_screenshot('导入电围JQDW0010用例执行失败')
        sleep(1)



if __name__ == '__main__':
    unittest.main()